package com.fiveflavors.suittar.gui.inputpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import com.fiveflavors.suittar.Suittar;
import com.fiveflavors.suittar.SuittarCommand;

public class SuittarInputStatusPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5996450834514396713L;
	private SuittarTextArea textArea = null;
	private JLabel lbl = null;
	private JButton btn = null;

	public SuittarInputStatusPanel() {
        
		setBackground(new Color(154,228,232));
        setPreferredSize(new Dimension(500,50));
        setMinimumSize(new Dimension(500,50));
        setMaximumSize(new Dimension(500,50));

	    GridBagLayout layout = new GridBagLayout();
	    setLayout(layout);
	    
        GridBagConstraints gbc = new GridBagConstraints();
        
        //add JTextArea
		textArea = new SuittarTextArea();
		textArea.setLineWrap(true);

		JScrollPane jsp = new JScrollPane();
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setPreferredSize(new Dimension(380,30));
		jsp.setMinimumSize(new Dimension(380,30));
		jsp.setMaximumSize(new Dimension(380,30));
        jsp.getViewport().setView(textArea);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(3,3,3,3);
        gbc.weightx = 100.0;
        gbc.weighty = 100.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        layout.setConstraints(jsp, gbc);
        add(jsp);
        
        lbl = new JLabel("What are you doing?");
        lbl.setFont(new Font("SansSerif",  Font.PLAIN,10));
        lbl.setForeground(new Color(0,102,204));
        lbl.setHorizontalAlignment(SwingConstants.CENTER);

        btn = new JButton("Update");
        btn.setBackground(new Color(154,228,232));
        
        JPanel pnl = new JPanel();
        pnl.setLayout(new GridLayout(2,1));
        pnl.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        pnl.add(lbl);
        pnl.add(btn);

        pnl.setBackground(new Color(154,228,232));
        pnl.setPreferredSize(new Dimension(120,50));
        pnl.setMinimumSize(new Dimension(120,50));
        pnl.setMaximumSize(new Dimension(120,50));
        gbc.gridx = 1;
        gbc.weightx = 0;
        gbc.weighty = 100.0;
        gbc.insets = new Insets(0,0,0,0);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.SOUTHEAST;
        layout.setConstraints(pnl, gbc);
        add(pnl);

	}
	
	public void addButtonActionListener(Suittar suittar) {
		btn.addActionListener(new SuittarStatusUpdateActionListener(suittar,SuittarCommand.COMMAND_UPDATE_STATUS));
	}
	
	public void addEnterActionListener(Suittar suittar) {
		textArea.getDocument().addDocumentListener(new SuittarStatusUpdateActionListener(suittar,SuittarCommand.COMMAND_UPDATE_STATUS));
	}
	
	public String getText() {
		return textArea.getText();
	}

	public void clear() {
		textArea.setText("");
	}
	
	public void setAtMarkUser(String user) {
		textArea.insert("@" + user, textArea.getCaretPosition());
	}
	
	public void setTextAreaEditable(boolean editable) {
		textArea.setEditable(editable);
	}

}
